package web.servlet;

import base.BaseServlet;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import pojo.Course;
import pojo.Course_Section;
import service.iml.ServiceDao;
import service.iml.ServiceDaoImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/course")
public class CourseServlet extends BaseServlet {
      ServiceDao sd = new ServiceDaoImpl();
    public void findCourseList(HttpServletRequest request, HttpServletResponse response){
        try {
            List<Course> courseList = sd.findCourseList();
            SimplePropertyPreFilter sppf = new SimplePropertyPreFilter(Course.class,"id",
                    "course_name","price","sort_num","status");
            String toJSONString = JSON.toJSONString(courseList, sppf);
            response.getWriter().print(toJSONString);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void findByCourseNameAndStatus(HttpServletRequest request, HttpServletResponse response){
        try {
            String course_name = request.getParameter("course_name");
            String status = request.getParameter("status");
            List<Course> byCourseNameAndStatus = sd.findByCourseNameAndStatus(course_name, status);
            SimplePropertyPreFilter sppf = new SimplePropertyPreFilter(Course.class,"id",
                    "course_name","price","sort_num","status");
            String s = JSON.toJSONString(byCourseNameAndStatus, sppf);
            response.getWriter().print(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void findCourseById(HttpServletRequest request, HttpServletResponse response) {
        try {
            String id = request.getParameter("id");
            Course course = sd.findCourseById(Integer.parseInt(id));
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class,"id",
                    "course_name","brief","teacher_name","teacher_info","preview_first_field","preview_second_field"
            ,"discounts","price","price_tag","course_img_url","share_title","share_image_title","share_description",
                    "course_description","status");
            String result = JSON.toJSONString(course,filter);
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void updateCourseStatus(HttpServletRequest request, HttpServletResponse response){
        try {
            String id = request.getParameter("id");
            String status = request.getParameter("status");
            String s = sd.updateCourseStatus(Integer.parseInt(id), Integer.parseInt(status));
            String result = JSON.toJSONString(s);
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }





}
